package main

import (
	"fmt"
	//"github.com/golang/protobuf/proto"
	"golang.org/x/net/context"
	"google.golang.org/grpc"
	"io"
	"Gocode/study.com/RPCandgRPC/grpc/Bidirectionalflow/model"
	//"net/rpc"
)

func main() {

	names := []string{"小明"}
	//genders := []string{"男", "女"}
	userMap := map[string]model.UserRequest{
		"1":model.UserRequest{Name: names, Age: 20, Gender: 0},
		"2":model.UserRequest{Name: []string{"小花"}, Age: 20, Gender: 1},
	}
	//连接服务端
	conn, err := grpc.Dial("localhost:9000", grpc.WithInsecure())
	if err != nil {
		//panic(err.Error())
		fmt.Println("grpc.Dial error=", err)
		return
	}
	defer conn.Close()

	gRPCClient := model.NewGetAndAddUserServiceClient(conn)

	getAndAddUser, err := gRPCClient.GetAndAddUser(context.Background())
	if err != nil {
		//panic(err.Error())
		fmt.Println(" gRPCCliect.GetUser error=", err)
		return
	}
	for _, val := range userMap {
		err = getAndAddUser.Send(&val)
		if err != nil {
			//panic(err.Error())
			fmt.Println(" gRPC Client addUser.Send error=", err)
			return
		}
	}
	for {
		recv, err := getAndAddUser.Recv()
		if err == io.EOF{
			fmt.Println("读取结束")
			return
		}
		if err!=nil{
			fmt.Println("client Recv error=", err)
		}

		fmt.Println(recv)
		fmt.Println(recv.GetAge())
		//if recv.GetAge() == 0{
		//	// 发送完毕
		//	return
		//}
	}
}
